What is REST API / Representation State Transfer
-
Restful Web Server/Application? Web application that implements
HTTP CRUD methods in Restful way. Eg: Twitter, facebook are implemented using restful APIs.
When web-client calls Restful APIs then Web-Server can return JSON or HTML or XML
How REST Works?
1. Web server(abc.com) defines Resources(Eg: Customers, movies, cars etc) and exposes a service/End point
eg:(https://abc.com/api/movies), where
abc.com : Domain
api : Convention(mostly used by companies) to expose RESTful services
movies : Resource
2. Web client will use above END POINT(https://abc.com/api/movies) to talk to Web service to perform CRUD Operations on Resource
3. Perform CRUD Operations on Resource.
Web Client Web Service
GET www.abc.com/api/movies HTTP/1.1 ---------> Query list of movies
<-[ {id:1,'movie1'}, {id:2,'movie2'} ]-
GET www.abc.com/api/movies/1 HTTP/1.1 ---------> return id=1 movie
<-[ {id:1,'movie1'} ]-
POST www.abc.com/api/movies {id:3, 'movie3'} HTTP/1.1 ---------> Add object [ {id:1,'movie1'}, {id:2,'movie2'}, {id:3,'movie3'} ]
PUT www.abc.com/api/movies/1 {id:1, 'movie11'} HTTP/1.1 ---------> Updates db [ {id:1,'movie11'}, {id:2,'movie2'}, {id:3,'movie3'} ]
DELETE www.abc.com/api/movies/1 HTTP/1.1 ---------> Delete object [ {id:2,'movie2'}, {id:3,'movie3'} ]
Client Web Service
http:///api/resource-name
Implement APIs GET(){..} POST(){..} PUT(){..} DELETE(){..}
<---data----
Why RESTful?
-
1. Rest makes Web Service OS Independent: Web applications can reside on different operating systems,
some could be on Windows, and others could be on Linux.
2. Rest makes Web Service language independent: Web services can be implemented in any language(C++,RUST etc)
3. On Cloud: RESTful applications are moved can reside on cloud. Azure and Amazon provide a lot of API’s based on the Restful architecture.
Comparison
REST vs HTTP
REST | HTTP | |
---|---|---|
What | Set of rules | L7 Protocol |
Focus | Resource | Methods to get |
REST vs SOAP(Simple Object Access Protocol)
REST | SOAP | |
---|---|---|
Why | To access web service | same |
Flexible | more | Less, hard to use |
Relies on | JSON,XML,CSV,RSS | XML, CORBA(Common Object Request Broker Architecture) rely on binary messaging |
REST vs gRPC
REST | gRPC | |
---|---|---|
What | Method to implement API | same |
size,speed | less(uses json) |
Better. Uses Protocol Buffers (protobuf) as its interface definition language. This binary serialization format is more efficient in terms of size and speed |
Communication | 1 direction | Bidirectional. both the client and server to send a stream of messages |
Efficiency | HTTP/1.1 | More. uses HTTP/2 for transport |
Versioning
-
What? Version numbers (like v1, v2, etc.) added to REST APIs
Why? Allows developers to introduce new features, deprecate old ones, and make modifications without disrupting existing applications
Eg: new authentication methods, performance optimizations, security methods might be added to v2
How versioning is implemented
URL Path Versioning (Most Common, easy to identify)
- version number is embedded in the URL path, such as /api/v1/resource
GET /api/v1/users
GET /api/v2/users
Header-Based Versioning (less visible)
- version information is provided in the request headers
GET /api/users
Header: API-Version: v1
Query Parameter Versioning
GET /api/users?version=1
Accept Header Versioning (Media Type)
- Sometimes, the Accept header is used to specify the version
application/vnd.example.v1+json
Code Implementations
-
Python flask-restful
Rust Actix_web Http Server exposing REST APIs